Skip to content

TYP: Add type annotations to Index (pandas/core/indexes/base.py)#64573

Open
jbrockmendel wants to merge 6 commits intopandas-dev:mainfrom
jbrockmendel:typ-indexes-base
Open

TYP: Add type annotations to Index (pandas/core/indexes/base.py)#64573
jbrockmendel wants to merge 6 commits intopandas-dev:mainfrom
jbrockmendel:typ-indexes-base

Conversation

@jbrockmendel
Copy link
Member

@jbrockmendel jbrockmendel commented Mar 13, 2026

This one was a slog getting working.

Summary

  • Add parameter and return type annotations throughout Index in pandas/core/indexes/base.py, enabling both pyright and mypy type checking for the module
  • Remove base.py from pyright's exclude list in pyright_reportGeneralTypeIssues.json
  • Expand the pandas.core.indexes.* mypy wildcard in pyproject.toml into individual module entries, excluding base so it gets checked
  • Add __getitem__ overloads (SupportsIndex -> Any vs slice | np.ndarray | list[int] | list[bool] | Series -> Index) to enable precise return types for callers
  • Tighten parameter types where possible (e.g. Hashable for label params, Axes for index-castable params, Frequency for freq params)
  • Fix _raise_if_missing and _union docstrings to match their actual signatures
  • Fix spillover type: ignore / pyright: ignore comments in ~28 other files caused by the newly precise types

Test plan

  • mypy passes with 0 errors on pandas/core/indexes/base.py (and all spillover files)
  • pyright passes with 0 errors on pandas/core/indexes/base.py
  • All pre-commit hooks pass (ruff, ruff-format, etc.)
  • CI type checking passes
  • Existing test suite passes (no runtime changes)

🤖 Generated with Claude Code

Copy link
Member

@mroeschke mroeschke left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: For AI to tackle typing, it might be easier to tackle the more "downstream" files first (e.g. groupby, reshaping, etc.) then tackle more upstream functionality (core data structures, internals)


@cache_readonly
def _na_value(self):
def _na_value(self) -> object:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we narrow this to Scalar?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Scalar doesnt include pd.NA, and would also break on 3rd partys (which id be ok not worrying about). I think Hashable is accurate, will update to that

@jbrockmendel
Copy link
Member Author

suggestion: For AI to tackle typing, it might be easier to tackle the more "downstream" files first (e.g. groupby, reshaping, etc.) then tackle more upstream functionality (core data structures, internals)

This was AI-written, but in this case I had to go through it line-by-line to convince claude to get things right. Per the OP comment, it was a real slog. Better to do it just once than discard and have to do again down the road.

As to the order to do things, I'm pretty sure bottom-up is correct, but will give it a try with some downstream modules and see how it goes.

@jbrockmendel
Copy link
Member Author

jbrockmendel commented Mar 13, 2026

This needs rebase following #64572.

Update: updated

jbrockmendel and others added 6 commits March 13, 2026 10:02
Add parameter and return type annotations throughout Index, enabling
both pyright and mypy type checking for the module. Remove base.py
from pyright's exclude list and expand the mypy indexes wildcard to
check base individually.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Move misplaced type: ignore comments to correct lines in tile.py and
base.py, remove stale ignores in resample.py and holiday.py, and add
missing mypy ignores in holiday.py.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Add pyright: ignore comments for reportGeneralTypeIssues,
reportArgumentType, and reportReturnType in base.py. Restore
type: ignore[assignment] for kurtosis alias in frame.py.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
maybe_unbox_numpy_scalar and cast_scalar_indexer were annotated in
GH#64572, making these ignores unnecessary.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@jbrockmendel
Copy link
Member Author

Trying this on reshape/window downstream modules and finding a) I don't know them nearly as well as indexes.base and b) im not as motivated to do the necessary arm-twisting to get things Just Right.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants